package cn.dsp.admin.controller;

import cn.dsp.admin.entity.Permission;
import cn.dsp.admin.service.IPermissionService;
import cn.dsp.admin.service.ex.ServiceException;
import cn.dsp.admin.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("perm")
public class PermissionController extends BaseController {
    @Autowired
    private IPermissionService service;

    /**
     * 添加
     * http://localhost:8088/perm/add/?name=perm/add&remarks=添加权限
     */
    @RequestMapping("add")
    public JsonResult<Void> addPerm(Permission permission) {
        try {
            service.insertPerm(permission);
        } catch (ServiceException e) {
            return new JsonResult<>(e);
        }
        return new JsonResult<>(SUCCESS);
    }

    /**
     * 查询
     * http://localhost:8088/perm/get_perm_by/1
     */
    @RequestMapping("get_perm_by/{pid}")
    public JsonResult<Permission> getPermByPid(@PathVariable("pid") Integer pid) {
        Permission result = null;
        try {
            result = service.getByPId(pid);
        } catch (ServiceException e) {
            return new JsonResult<>(e);
        }
        return new JsonResult<>(SUCCESS, result);

    }

    /**查询链表
     * http://localhost:8088/perm/get_perm_list
     * */
    @RequestMapping("get_perm_list")
    public JsonResult<List<Permission>> getPermAll() {
        List<Permission> perms = service.getPermAll();
        return new JsonResult<>(SUCCESS,perms);
    }

    /**修改
     * http://localhost:8088/perm/change_perm_info?pid=1&name=get_perm_list&remarks=查询
     * */
    @RequestMapping("change_perm_info")
    public JsonResult<Permission> changePerm(Permission permission){
        try {
            service.changePermByPid(permission);
        } catch (ServiceException e) {
            return new JsonResult<>(e);
        }
        return new JsonResult<>(SUCCESS);
    }

    /**删除
     * http://localhost:8088/perm/delete_perm_by/5
     * */
    @RequestMapping("delete_perm_by/{pid}")
    public JsonResult<Void> deletePermByPid(@PathVariable("pid") Integer pid){
        try {
            service.deletePermByPid(pid);
        } catch (ServiceException e) {
            return new JsonResult<>(e);
        }
        return new JsonResult<>(SUCCESS);
    }

}
